export function getScreenSize() {
  const clientWidth = document.documentElement.clientWidth
  const clientHeight = document.documentElement.clientHeight
  const width = clientWidth > 512 ? 512 : clientWidth
  const height = clientHeight > 768 ? 768 : clientHeight
  return {
    width,
    height
  }
}

/**
 * 碰撞检测
 * @param {Object} objA 对象1 
 * @param {Object} objB 对象2
 */
export const hitTestObject = (objA, objB) => {
  return (
    (objA.x + objA.width >= objB.x) &&
    (objA.y + objA.height >= objB.y) &&
    (objB.x + objB.width >= objA.x ) &&
    (objB.y + objB.height >= objA.y)
  )
}

/**
 * 节流函数
 * @param {Function} fn 
 * @param {Number} delay 
 * @returns 
 */
export const throttle = (fn, delay = 200) => {
  let timer
  return function() {
    if(!timer) {
      timer = setTimeout(() => {
        timer = null
        fn.call(this, ...arguments)
      }, delay)
    }
  }
}

// 是否是Mobile端
export const isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
